package com.lsmgc.cms.util.upload;

import com.lsmgc.cms.util.RequestUtils;
import com.lsmgc.cms.util.ServletUtils;
import com.lsmgc.cms.util.dto.ParamDto;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UploadServlet extends HttpServlet {
    private UploadService uploadService = new UploadService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/ajax/upload/img":
                uploadImg(request, response);
                break;
        }
    }

    private void uploadImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ParamDto paramDto = RequestUtils.parseParam(request);
        String url=uploadService.uploadImg(paramDto.getFileMap().get("image"));
        ServletUtils.renderJson(response, ServletUtils.getYesJson(url));
    }
}
